package DP;

/**
 * 一列数组，i 买入，j 卖出，求最大
 */
public class Stock {

    /**
     * 买一次卖一次
     */
    public int maxProfit(int[] prices) {
        int n = prices.length;
        if(n == 0){
            return 0;
        }
        int minV = prices[0];
        int max = 0;

        for(int i=1; i < n; i++){
            max = Math.max(max, prices[i] - minV);
            minV = Math.min(minV, prices[i]);
        }
        return max;
    }

    //不限次数买卖，最大贪心
    public int maxProfit2(int[] prices) {
        int n = prices.length;
        if(n == 0){
            return 0;
        }
        int max = 0;

        for(int i=1; i < n; i++){
            //碰到下降的就在昨天卖掉
            if(prices[i] > prices[i-1]){
                max += prices[i] - prices[i-1];
            }
        }
        return max;
    }

    //限定只能2次操作
    public int maxProfit3(int[] prices) {
        int n = prices.length;
        if(n == 0){
            return 0;
        }
        int max = 0;

        for(int i=1; i < n; i++){
            //碰到下降的就在昨天卖掉
            if(prices[i] > prices[i-1]){
                max += prices[i] - prices[i-1];
            }
        }
        return max;
    }

    public static void main(String[] args){
        int[] n = new int[]{-1,1,1,1};

    }


}
